﻿<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
<div id="minicalendar" style="width:170px;padding-left:2px;margin-top:30px;"></div>
<script type="text/javascript">
    var events;
    

    // Receives January->1
    function addTipsys(year, month, calendarId) {
        
        //var theEvents = getMonthEvents(year, month, calendarId);
        var theDateLinks = $('#' + calendarId + ' .ui-datepicker-calendar a');
        for (var i = 0; i < events.length; i++) {
            var date = new Date(parseInt(events[i].StartDate.substr(6)));
            if (date.getMonth() == month && date.getFullYear() == year) {
  
                theDateLinks.eq(date.getDate() - 1)
                    .attr('original-title',
                    events[i].Title
                    + '<br>' + date.toString().substring(0,21)//formatDate(date,"yyyy-MM-dd HH:mm")
                    + (events[i].Message != null ? '<br>' + events[i].Message : '')
                    + (events[i].Course != '' ? '<br>' + events[i].Course : '')
                    + (events[i].User != '' ? '<br>' + events[i].User : '')
                    )
                    .tipsy({ gravity: 'se', fade: true,html:true/*,21
                        title: function () {
                            var displayData = "";
                            displayData += "<br><b>" + events[i].Title + "</b><br><br>";
                            displayData += "<b>Event Date:</b> " + events[i].StartDate + "<br>";
                            if (events[i].Message != 'null' && events[i].Message != null)
                                displayData += "<b>" + "Message" + ":</b> " + events[i].Message + "<br>";
                            if (events[i].Course != null)
                                displayData += "<b>" + "Course" + ":</b> " + events[i].Course + "<br>";
                            if (events[i].User != 'null' && events[i].User != null)
                                displayData += "<b>" + "User" + ":</b> " + events[i].User + "<br>";
                            return displayData;
                        }*/

                    });  
            }
            
        }
    }
    function highlightDays(date) {
        for (var i = 0; i < events.length; i++) {//
            var event = new Date(parseInt(events[i].StartDate.substr(6)));
            event.setHours(0);
            event.setMinutes(0);
            event.setSeconds(0);
            event.setMilliseconds(0);

            if (event - date == 0/*event.getFullYear() == date.getFullYear() && event.getMonth() == date.getMonth() && event.getDate() == date.getDate()*/) {
                return [true, 'ui-state-highlight'];
                }
        }
                return [true, ''];
    }
    $(document).ready(function () {
        jQuery.ajax({
            type: 'POST',
            url: '<%= Url.Action("GetEvents", "CalendarEvent") %>',
            contentType: 'application/json; charset=utf-8',
            success: function (data) {
                events = data;
                $("#minicalendar").datepicker({ beforeShowDay: highlightDays });
                // Gets the date and initializes the first round of tipsies.
                var currentDate = $('#minicalendar').datepicker('getDate');
                // month+1 because the event considers January->1
                // Last element is null, because, it doesn't actualy get used in the hanlder.
                addTipsys(currentDate.getFullYear(), currentDate.getMonth(), 'minicalendar');

                var _updateDatepicker_o = $.datepicker._updateDatepicker;
                // Replaces the function.
                $.datepicker._updateDatepicker = function (inst) {
                    // First we call the original function from the appropiate context.
                    _updateDatepicker_o.apply(this, [inst]);
                    // No we can update the Tipsys.
                    addTipsys(inst.drawYear, inst.drawMonth, inst.id);
                };
            },
            statusCode: {
                404: function () {
                    alert('page not found');
                }
            }
        });
    });
    /*
    (function ($) {

        var events;

        function getMonthEvents(year, month, calendarId) {
            return { 11: "My birthday.", 23: "My anniversary" };
        }

        // Receives January->1
        function addTipsys(year, month, calendarId) {
            alert(events);
            //var theEvents = getMonthEvents(year, month, calendarId);
            var theDateLinks = $('#' + calendarId + ' .ui-datepicker-calendar a');
            for (eventDay in events) {
                alert(eventDay.StartDate.getMonth() + ' ' + month + '' + eventDay.StartDate.getYear() + '' + year);
                if (eventDay.StartDate.getMonth() == month && eventDay.StartDate.getYear() == year) {

                    theDateLinks.eq(eventDay.StartDate.getDate() - 1)
                    .attr('original-title', eventDat.Title)
                    .tipsy();
                }
            }
        }

        // Because the the event `onChangeMonthYear` get's called before updating 
        // the items, we'll add our code after the elements get rebuilt. We will hook 
        // to the `_updateDatepicker` method in the `Datepicker`.
        // Saves the original function.
        var _updateDatepicker_o = $.datepicker._updateDatepicker;
        // Replaces the function.
        $.datepicker._updateDatepicker = function (inst) {
            // First we call the original function from the appropiate context.
            _updateDatepicker_o.apply(this, [inst]);
            // No we can update the Tipsys.
            addTipsys(inst.drawYear, inst.drawMonth + 1, inst.id);
        };

        // Finally the calendar initializer.
        $(function () {
            // Creates the date picker, with your options.
            $("#minicalendar").datepicker();
            // Gets the date and initializes the first round of tipsies.
            var currentDate = $('#minicalendar').datepicker('getDate');
            // month+1 because the event considers January->1
            // Last element is null, because, it doesn't actualy get used in the hanlder.
            addTipsys(currentDate.getYear(), currentDate.getMonth() + 1, 'minicalendar');
        });

    })(jQuery);**/
</script>
